<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/2
 * Time: 15:26
 */
class DeviceAction extends BaseAction{
    private $devices;
    private $classname = 'device';
    private $table_company = 'device_company';
    private $table_type = 'device_type';
    private $table_status = 'device_status';
    private $table_brand = 'device_brand';
    private $table_cq = 'device_cq';
    private $page_size = 30;
    private $page = 1;
    private $pur = array();
    function _initialize(){
        parent::_initialize();
        $this->devices=new device();
    }
    function search(){
        $tid=Req::get('tid')?Req::get('tid'):0;
        $title='所有设备';
        if($tid){
            $types=$this->devices->get_type_one($tid);
            $title=$types->name;
        }
        //存放地点
        $list = $this->devices->get_list('device_loc');
        $this->assign('list_loc', $list);

        $this->assign('tid',$tid);
        $this->assign('title',$title.'查询');
        $this->display('device_search');
    }
    function lists(){
        $page=Req::get('page')?Req::get('page'):1;
        $page_size=30;
        $tid=Req::get('tid');
        $name=Req::get('name');
        $device_no=Req::get('device_no');
        $ip=Req::get('ip');
        $person=Req::get('person');
        $location=Req::get('location');
        $company_id=$this->curr_company();
        $where='';
        if($name)$where.=" and d.name like '%{$name}%'";
        if($device_no)$where.=" and d.device_no like '%{$device_no}%'";
        $arr_type=array('2,3,9,10,11');
        if($person && !in_array($tid,$arr_type))$where.= " and  d.use_user_name like '%{$person}%' ";
        if($person && in_array($tid,$arr_type))$where.= " and  d.person like '%{$person}%' ";
        if($location)$where.=" and d.location ='{$location}'";
        if($ip)$where.=" and d.ip=inet_aton({$ip}) ";
        if($this->info['roleid']>1)$where.=" and d.company_id={$company_id} and dtr.role &". READ;
        $sql="select count(d.id) as total from __device d left join __device_type dt on d.type_id=dt.id
 left join __device_type_role dtr on dt.id=dtr.type_id where d.type_id={$tid}".$where.' group by d.id';
        $total=db::get_one($sql);
        $ac=db::get_one('SELECT FOUND_ROWS() as count');
        $total=$ac->count;
        $this->assign('total',$total);
        if($total>0) {
            $sql = "select d.id,d.product_date,d.person,d.status_id,d.name,d.model,b.name as brand_name from __device d left join __device_type dt on d.type_id=dt.id
 left join __device_type_role dtr on dt.id=dtr.type_id left join __device_brand b on d.brand_id=b.id where d.type_id={$tid} " . $where.' group by d.id' . ' limit ' . (($page - 1) * $page_size) . ',' . $page_size;

            $list = db::get_all($sql);
            //  var_dump($list);
            $this->assign('list', $list);
        }
        $this->assign('tid',$tid);
        $this->assign('status',$this->devices->status);
        $this->assign('title','设备查询');
        $this->display('lists');

    }
    function is_READ($tid){
        $sql='select * from __device_type_role dtr left join __device_type dt on dtr.type_id=dt.id where group_id='.$this->info['roleid'].' and dtr.type_id='.$tid;
        $role=db::get_one($sql);
        //var_dump($role);
    }
    function curr_company(){
        $sql='select ifnull(company_id,0) as id from __role where roleid='.$this->info['roleid'];
        $res=db::get_one($sql);
        return $res->id;
    }
    function view(){
        if($_POST){
            return $this->mod();
        }
        $id=Req::get('id')?Req::get('id'):exit('错误的参数!');
        $o=Req::get('o')?1:0;
        $this->assign('id',$id);
        $sql='select d.*,inet_ntoa(d.ip) as ip,t.table,t.name as type_name,b.name as brand_name,cq.name as cq_name,dtr.role from __device d
 left join __device_type t on t.id=d.type_id
left join __device_brand b on b.id=d.brand_id

left join __device_cq cq on cq.id=d.cq_id
left join __device_type_role dtr on t.id=dtr.type_id where d.id='.$id;//left join __device_company c on c.id=d.company_id
        $view=db::get_one($sql);
        //var_dump($view);
        $this->assign('oj',$view);


        //设备状态
        $this->assign('list_status',$this->devices->status);
        //品牌
        $list = $this->devices->get_list($this->table_brand);
        $this->assign('list_brand', $list);
        //产权归属
        $list = $this->devices->get_list($this->table_cq);
        $this->assign('list_cq', $list);
        //领用部门
        $list = $this->devices->get_list('device_department');
        $this->assign('list_department', $list);
        //存放地点
        $list = $this->devices->get_list('device_loc');
        $this->assign('list_loc', $list);
        //资产性质
        $list = $this->devices->get_list('device_xz');
        $this->assign('list_xz', $list);
        //操作类型及版本号
        $list = $this->devices->get_list('device_os');
        $this->assign('list_os', $list);
        //所属网络
        $list = $this->devices->get_list('device_net');
        $this->assign('list_net', $list);

        //获取附加表的内容
        if($view->table) {
            $type = array();
            $addon = $this->devices->get_addon_one($view->table, $id,'assoc');
            $this->assign('addon',$addon);
            $this->assign('field', $this->devices->get_device_addon_fields($view->table));
        }
        $this->assign('title','设备信息查询');
        /*-----查询是否有新的修改-------*/
        if(!$o) {
            //$sql = 'select * from __device_mod where status=0 and device_id=' . $id . ' order by id desc limit 1';
            //$new_mod = db::get_one($sql);
            $new_mod=$this->devices->get_new_mod($id);
            if ($new_mod) {
                $this->assign('new_mod_time',date('Y-m-d H:i:s',$new_mod->update_time));

                $this->assign('new_mod', $new_mod->con[0]);
                $this->assign('new_addon', $new_mod->con[1]);
            }
        }
        if($view->table){
            $this->display('view_'.$view->table);
        }else {
            $this->display('view');
        }
    }
    function add(){
        $id=Req::get('id','intvale')?Req::get('id','intvale'):exit('错误的参数');

        //设备状态
        $this->assign('list_status',$this->devices->status);
        //品牌
        $list = $this->devices->get_list($this->table_brand);
        $this->assign('list_brand', $list);
        //产权归属
        $list = $this->devices->get_list($this->table_cq);
        $this->assign('list_cq', $list);
        //领用部门
        $list = $this->devices->get_list('device_department');
        $this->assign('list_department', $list);
        //存放地点
        $list = $this->devices->get_list('device_loc');
        $this->assign('list_loc', $list);
        //资产性质
        $list = $this->devices->get_list('device_xz');
        $this->assign('list_xz', $list);
        //操作类型及版本号
        $list = $this->devices->get_list('device_os');
        $this->assign('list_os', $list);
        //所属网络
        $list = $this->devices->get_list('device_net');
        $this->assign('list_net', $list);


        $t=$this->devices->get_type_one($id);
        if($t->table){
            $field=$this->devices->get_device_addon_fields($t->table);
            $this->assign('field',$field);
        }
        $this->assign('tid',$id);
        $this->display('view');
    }
    function mod(){
        $id=Req::post('id');
        $tid=Req::post('tid');
        $new_post=Req::post('n');
        $old_post=Req::post('o');
        $addon_post=Req::post('addon');
        $o_addon=Req::post('o_addon');
        $data=array();
        foreach($new_post as $k=>$v){
            if($v){
                if($old_post[$k]!=$v)$data[0][$k]=$v;
            }
        }
        foreach($addon_post as $k=>$v){
            if($v && $o_addon[$k]!=$v)$data[1][$k]=$v;
        }
        if($data[0]['product_no'])$data[0]['product_no']=strtoupper($data[0]['product_no']);
        if($data[0]['record_no'])$data[0]['record_no']=strtoupper($data[0]['record_no']);
        if($data[1]['mac'])$data[1]['mac']=strtoupper($data[1]['mac']);


        if($tid && !$id){
            $sql='select company_id from __role where roleid='.$this->info['roleid'];
            $company=db::get_one($sql);
            if(is_array($data[0])){
                $data[0]['company_id']=$company->company_id;
                $data[0]['type_id']=$tid;


                if($this->devices->is_obj($data[0]['obj_id'])){
                    show_msg('已有此OBJ_ID');
                }


                db::_insert('device',$data[0]);
                $last_id=db::last_id();
                if(!$last_id){
                    $this->msg(2,'添加失败');
                }else {
                    if (is_array($data[1])) {
                        $t = $this->devices->get_type_one($tid);
                        $data[1]['device_id'] = $last_id;

                        if ($t->table)
                            if (db::_insert('device_' . $t->table, $data[1])) {
                                $this->msg(1, '添加成功', '/');
                            } else {
                                $this->msg(2, '添加附加表失败');
                            }
                    }
                    $this->msg(1, '添加成功', '/');
                }


            }
        }else {
            if (is_array($data[0]) || is_array($data[1])) {
                $in_data = array('uid' => $this->info['masterid'], 'device_id' => $id, 'update_time' => time(), 'con' => serialize($data));
                if (db::_insert('device_mod', $in_data)) {
                    $this->msg(1,'修改成功','/');
                } else {
                    $this->msg (2,'修改出现问题了');
                }
            } else {
                $this->msg(2,'您没有做任何修改');
            }
        }
    }
    function msg($s,$msg,$url){
        echo json_encode(array('state'=>$s,'msg'=>$msg,'url'=>$url));
    }
}